import $ from "jquery";
import {url} from "../../util/config";
import {message} from "antd";
import login from "../login";

function getNewLogin(searchKey=null,startDate=null,endDate=null,current=null,pageSize=null) {
    return getActivity(searchKey,startDate,endDate,current,pageSize)
}

export default function getActivity(searchKey=null,startDate=null,endDate=null,current=null,pageSize=null) {
    let status = {
        data:null,
        count:null
    }
    let token = localStorage.getItem("token")
    $.ajax({
        async:false,
        headers: {
            'Accept': "application/json",
            'Authorization': token
        },
        // 允许请求携带cookie
        // xhrFields: {
        //     withCredentials: true
        // },
        url: url.api_url+`Activity/GetActivityList?searchKey=${searchKey}&startDate=${startDate}&endDate=${endDate}&current=${current}&pageSize=${pageSize}`,
        type: 'GET',
        success: function (result) {
            let tmp = result.result;
            if(tmp=="Token過期請重新登錄"){
                login(localStorage.getItem("account"),localStorage.getItem("password"));
                status = getNewLogin(searchKey,startDate,endDate,current,pageSize)
            }else {
                if(result.result=="no permission"){
                    status.data = result.result
                }else {
                    status.data = tmp.map(item=>{
                        return {
                            new:item.ARTICLE_NEW,
                            key:item.ID,	//活动id
                            figure:item.ACTIVITY_IMAGE,  //活动配图
                            name:item.NAME,	//活动名称
                            status:item.TOP_STATUS,		//置顶 ？true : false
                            createUser:item.CREATE_USER,
                            createTime:item.CREATE_DATE,
                            // sortNum:item.SORT_NUM,		//如果置顶为false,该字段必须有值
                            updateUser:item.UPDATE_USER,
                            updateTime:item.UPDATE_DATE
                        }
                    })
                    status.count = result.count
                }
            }
            // sessionStorage.setItem("activityArr",JSON.stringify(activityArr))
        },
        error:function (error) {
            message.error("请求异常")
        }
    })
    return status;

}

